package juc.juccore.threadlocal;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadLocalNormalUsage05 {
    public static ExecutorService executorService = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 100; i++) {
            int a = i;
            executorService.submit(new Thread(() -> {
                String data = new ThreadLocalNormalUsage01().data(a * 10);
                System.out.println(data);
            }));
        }
        executorService.shutdown();
    }

    public String data(int seconds) {
        Date date = new Date(1000 * seconds);
        SimpleDateFormat dataFormat = ThreadSafeFormatter.dateFormatThreadLocal.get();
        return dataFormat.format(date);

    }
}

class ThreadSafeFormatter {
    public static ThreadLocal<SimpleDateFormat> dateFormatThreadLocal = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };
    public static ThreadLocal<SimpleDateFormat> dateFormatThreadLocal2 = ThreadLocal.withInitial(
            () -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

}
